package com.ikas.ai.server.module.morningpaper.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.consts.MachineAndWorkConditionConst;
import com.ikas.ai.server.module.morningpaper.consts.MorningPaperOverhaulVariableNames;
import com.ikas.ai.server.module.morningpaper.dao.MorningPaperOverhaulPlanDao;
import com.ikas.ai.server.module.morningpaper.model.dto.MorningPaperOverhaulDto;
import com.ikas.ai.server.module.morningpaper.model.po.MorningPaperOverhaulPlanPo;
import com.ikas.ai.server.module.morningpaper.util.MorningPaperTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author li.hexian
 * 检修记录相关接口及工具
 */
@Service
public class MorningPaperOverhaulPlanService extends ServiceImpl<MorningPaperOverhaulPlanDao, MorningPaperOverhaulPlanPo> {

    @Autowired
    MorningPaperTimeUtil morningPaperTimeUtil;

    /**
     * 返回机组编号列表
     * @return
     */
    public List<String> listMachineNos() {
        List<String> machineNos = new ArrayList<>();
        machineNos.add(MachineAndWorkConditionConst.MACHINE_01_NO);
        machineNos.add(MachineAndWorkConditionConst.MACHINE_02_NO);
        machineNos.add(MachineAndWorkConditionConst.MACHINE_03_NO);
        machineNos.add(MachineAndWorkConditionConst.MACHINE_04_NO);
        return machineNos;
    }

    /**
     * 将符合条件的Overhaul信息一次性查出
     * @return
     */
    public Map<String, List<MorningPaperOverhaulPlanPo>> getPosMap(){
        Date now = new Date();
        return list(
                new LambdaQueryWrapper<MorningPaperOverhaulPlanPo>()
                        .lt(MorningPaperOverhaulPlanPo::getStartTime, now)
                        .gt(MorningPaperOverhaulPlanPo::getEndTime, now)
        ).stream().collect(Collectors.groupingBy(MorningPaperOverhaulPlanPo::getMachineNo));
    }

    /**
     * 是否正在检修中
     * @param machineNo
     * @param posMap
     * @return
     */
    public String inOverhaulNow(String machineNo, Map<String, List<MorningPaperOverhaulPlanPo>> posMap){
        if (posMap.get(machineNo)!=null && posMap.get(machineNo).size()>0){
            MorningPaperOverhaulPlanPo one = posMap.get(machineNo).stream().sorted(Comparator.comparing(MorningPaperOverhaulPlanPo::getEndTime).reversed()).collect(Collectors.toList()).get(0);
            return generateOverhaulInfo(one);
        }
        else return null;
    }

    /**
     * 生成检修日期信息
     * @param one
     * @return
     */
    private String generateOverhaulInfo(MorningPaperOverhaulPlanPo one){
        SimpleDateFormat cnSdf = new SimpleDateFormat(morningPaperTimeUtil.getDateTimeFormat()+morningPaperTimeUtil.getTimeTimeFormat(), Locale.CHINA);
        String planStartTime = cnSdf.format(one.getStartTime());
        String planEndTime = cnSdf.format(one.getEndTime());
        return planStartTime+" - "+planEndTime;
    }

    /**
     * 保存检修记录
     * @param morningPaperOverhaulDto
     * @param userName
     */
    public MorningPaperOverhaulPlanPo savePlan(MorningPaperOverhaulDto morningPaperOverhaulDto,String userName) {
        MorningPaperOverhaulPlanPo morningPaperOverhaulPlanPo = BeanUtil.copyProperties(morningPaperOverhaulDto, MorningPaperOverhaulPlanPo.class);
        morningPaperOverhaulPlanPo.setCreator(userName);
        morningPaperOverhaulPlanPo.setUpdater(userName);
        morningPaperOverhaulPlanPo.setCreateTime(new Date());
        save(morningPaperOverhaulPlanPo);
        return morningPaperOverhaulPlanPo;
    }
    /**
     * 修改检修记录
     * @param morningPaperOverhaulDto
     * @param userName
     */
    public void updatePlan(MorningPaperOverhaulDto morningPaperOverhaulDto,String userName){
        MorningPaperOverhaulPlanPo morningPaperOverhaulPlanPo = BeanUtil.copyProperties(morningPaperOverhaulDto, MorningPaperOverhaulPlanPo.class);
        morningPaperOverhaulPlanPo.setUpdater(userName);
        updateById(morningPaperOverhaulPlanPo);
    }

    /**
     * 获取全部检修信息
     * @return
     */
    public Map<String,String> getAllMachineOverhaulInfo(){
        Map<String, List<MorningPaperOverhaulPlanPo>> posMap = getPosMap();
        Map<String, String> map = new HashMap<>();
        String machineU01State = inOverhaulNow(MachineAndWorkConditionConst.MACHINE_01_NO,posMap);
        if (StringUtils.isNotBlank(machineU01State)) {
            map.put(MorningPaperOverhaulVariableNames.machineU01InOverhaul.name(), String.valueOf(true));
        }else{
            map.put(MorningPaperOverhaulVariableNames.machineU01InOverhaul.name(),String.valueOf(false));
        }
        String machineU02State = inOverhaulNow(MachineAndWorkConditionConst.MACHINE_02_NO, posMap);
        if (StringUtils.isNotBlank(machineU02State)) {
            map.put(MorningPaperOverhaulVariableNames.machineU02InOverhaul.name(), String.valueOf(true));
        }else{
            map.put(MorningPaperOverhaulVariableNames.machineU02InOverhaul.name(),String.valueOf(false));
        }
        String machineU03State = inOverhaulNow(MachineAndWorkConditionConst.MACHINE_03_NO, posMap);
        if (StringUtils.isNotBlank(machineU03State)) {
            map.put(MorningPaperOverhaulVariableNames.machineU03InOverhaul.name(), String.valueOf(true));
        }else{
            map.put(MorningPaperOverhaulVariableNames.machineU03InOverhaul.name(),String.valueOf(false));
        }
        String machineU04State = inOverhaulNow(MachineAndWorkConditionConst.MACHINE_04_NO, posMap);
        if (StringUtils.isNotBlank(machineU04State)) {
            map.put(MorningPaperOverhaulVariableNames.machineU04InOverhaul.name(), String.valueOf(true));
        }else{
            map.put(MorningPaperOverhaulVariableNames.machineU04InOverhaul.name(),String.valueOf(false));
        }
        return map;
    }

    /**
     * 获取检修日期文本
     * @param machineNo
     * @return
     */
    public String getOverhaulDetailText(String machineNo) {
        Date now = new Date();
        return generateOverhaulInfo(getOne(
                new LambdaQueryWrapper<MorningPaperOverhaulPlanPo>()
                        .eq(MorningPaperOverhaulPlanPo::getMachineNo,machineNo)
                        .lt(MorningPaperOverhaulPlanPo::getStartTime, now)
                        .gt(MorningPaperOverhaulPlanPo::getEndTime, now)
                        .orderByDesc(MorningPaperOverhaulPlanPo::getEndTime)
                        .last("limit 1")
        ));
    }
}
